import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.List;
import java.util.Map;

/**
 * @author weixin.gong
 * @date 17-5-19 下午5:53
 */
public class MustacheExample {

    @Test
    public void compileString() throws IOException {
        Map<String, Object> context = Maps.newHashMap();
        context.put("name", "Michael");
        context.put("first", "RIP");
        MustacheFactory mf = new DefaultMustacheFactory();
        //Mustache mustache = mf.compile("template.mustache");//template.mustache资源名
        Mustache mustache = mf.compile(new StringReader("{{name}}{{first}}\r\n"), "template.mustache");
        mustache.execute(new PrintWriter(System.out), context).flush();
    }

    @Test
    public void compileFile() throws IOException {
        Map<String, Object> context = Maps.newHashMap();
        context.put("name", "Michael");
        context.put("first", "RIP");
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile("template.mustache");//template.mustache资源名
        mustache.execute(new PrintWriter(System.out), context).flush();
    }

    @Test
    public void override() throws IOException {
        //{"name":[{"first":"Michael"}],"first":"RIP"}
        //{{#name}}{{first}}{{/name}}模板的结果
        Map<String, Object> context = Maps.newHashMap();
        List<Map<String, String>> list = Lists.newArrayList();
        Map<String, String> name = Maps.newHashMap();
        name.put("first", "Michael");
        list.add(name);
        context.put("name", name);
        context.put("first", "RIP");
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile(new StringReader("{{#name}}{{first}}{{/name}}\r\n"), "template.mustache");
        mustache.execute(new PrintWriter(System.out), context).flush();
    }
}
